package com.noranyunxi.www.controller;

import com.noranyunxi.www.util.SeleniumUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class SeleniumController {
    /**
     * 传入 url 获取 html
     *
     * http://127.0.0.1:8624/page?url=https://mp.weixin.qq.com/s/ZGJSrNKSBFdav5y1EH_Vag
     *
     * @param params
     * @return
     */
    @GetMapping("/page")
    public Map<String, Object> page(@RequestParam Map<String, String> params) {
        String url = params.get("url").toString();
        Map<String, Object> resultMap = new HashMap<>();

        ChromeDriver chromeDriver = null;
        try {
            chromeDriver = SeleniumUtil.initChromeDriver(url);
            String pageSource = chromeDriver.getPageSource();

            resultMap.put("page", pageSource);

            Document document = Jsoup.parseBodyFragment(pageSource);
            Element js_content = document.getElementById("js_content");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            SeleniumUtil.quitChromeDriver(chromeDriver);
        }

        return resultMap;
    }
}
